package com.example.cgpsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.cgpsys.beans.Leader;
import com.example.cgpsys.beans.Result;
import com.example.cgpsys.dao.LeaderDao;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/leader")
@RestController
public class LeaderController {

    private final LeaderDao leaderDao;

    public LeaderController(LeaderDao leaderDao) {
        this.leaderDao = leaderDao;
    }

    @PostMapping("/add")
    public Result addLeader(@RequestBody Leader leader){
        leaderDao.insert(leader);
        return Result.success();
    }

    @GetMapping("/loadLeaderList")
    public Result loadList(Integer id){
        QueryWrapper<Leader> leaderQueryWrapper = new QueryWrapper<>();
        if (id!=null){
            leaderQueryWrapper.eq("id",id);
        }
        return Result.success(leaderDao.selectList(leaderQueryWrapper));
    }

    @GetMapping("/delete")
    public Result deleteLeader(Integer id){
        leaderDao.deleteById(id);
        return Result.success();
    }
}
